package com.esms.controller;

import com.esms.dao.EmpDepartmentCustomVoMapper;
import com.esms.dao.EmployeeMapper;
import com.esms.po.Department;
import com.esms.po.Employee;
import com.esms.service.DepartmentService;
import com.esms.utils.Message;
import com.esms.vo.EmpDepartmentCustomVo;
import com.esms.vo.EmpDepartmentPages;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpSession;

/**
 * 个性化指标Controller
 * @Author: ko
 * @DATE: 2022/2/17 15:23
 */

@Controller
@RequestMapping("/empDepartment")
public class EmpDepartmentController {
	
	@Autowired
    private EmployeeMapper employeeMapper = null;
	@Autowired
	private EmpDepartmentCustomVoMapper empDepartmentCustomVoMapper  = null;
	
	@Autowired
	public DepartmentService departmentService;

	/**
	 * 查找被评价部门
	 * @param pageNum
	 * @param limit
	 * @param d_name
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/findSelective.do")
    @ResponseBody
    public EmpDepartmentPages findSelective(
            @RequestParam(value="page", defaultValue="1")int pageNum,
            @RequestParam(value="limit", defaultValue="5") int limit,
            HttpSession httpSession) throws Exception {
        
        /**
		 * 
		 * select * from
		        quantitative a
		        left join department b on a.q_id1 = b.d_id
		        left join generality c on a.q_targetid = c.g_targetid
		        left join employee e on a.q_personid = e.e_account
			where a.q_id='3' and a.q_personid like ('%0000%')
			order by b.d_id desc;
		 * 
		 * **/
		
		//第一步：获取当前登录员工的ID,从缓存中直接取即可:e_id
		Integer eId = (Integer) httpSession.getAttribute("employeeId");
		//第二步：再获取当前员工对应的工号和部门ID,作为入参，查询：quantitative，就是q_personid和q_id 查询定量指标被打分的部门，显示到前台
		System.out.println("eId====="+eId);
		Employee employee = employeeMapper.selectByPrimaryKey(eId);
		String eAccount = employee.geteAccount();//当前登录员工的工号
		int dId = employee.getdId();//当前登录员工的部门
		
		//map里面的入参，要和mapper.xml文件中的条件一致
		HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("e_account",eAccount);
        map.put("d_id",dId);
        //map.put("e_name","");
        List<EmpDepartmentCustomVo> list = null;
        try {
        	//pageNum:起始页面  pageSize:每页的大小
            PageHelper.startPage(pageNum,limit);
            //查找条件，一定要紧跟在startPage后
            list =empDepartmentCustomVoMapper.selectQuantitativeSelective(map);
        }catch(Exception e) {
        	e.printStackTrace();
        }
        
        PageInfo pageResult = new PageInfo(list);
        //设置前台需要的数据
        EmpDepartmentPages employeePages = new EmpDepartmentPages();
        employeePages.setCode(0);
        employeePages.setMsg("");
        employeePages.setCount((int) pageResult.getTotal());
        employeePages.setData(pageResult.getList());
        return employeePages;
    }
}
